package com.smartfactory.sail.controller;

import com.ada.base.common.ApiResponse;
import com.ada.base.common.ControllerResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.smartfactory.sail.config.HeaderWrapper;
import com.smartfactory.sail.config.OperRecord;
import com.smartfactory.sail.constant.header.ListMaterialStorageEnum;
import com.smartfactory.sail.constant.header.ListProductsStorageEnum;
import com.smartfactory.sail.entity.OrderItem;
import com.smartfactory.sail.entity.StorageCurrentInfo;
import com.smartfactory.sail.entity.StorageRecord;
import com.smartfactory.sail.entity.enums.OrderItemStatusEnum;
import com.smartfactory.sail.entity.enums.StorageStatusEnum;
import com.smartfactory.sail.service.IOrderItemService;
import com.smartfactory.sail.service.IStorageCurrentInfoService;
import com.smartfactory.sail.service.IStorageRecordService;
import com.smartfactory.sail.utils.CommonUtils;
import com.smartfactory.sail.vo.PageParam;
import com.smartfactory.sail.vo.item.ListMaterialStorageResp;
import com.smartfactory.sail.vo.item.ListProductsStorageResp;
import com.smartfactory.sail.vo.order.ListOrderItemRsp;
import com.smartfactory.sail.vo.storage.ItemStorageInfo;
import com.smartfactory.sail.vo.storage.UpdateMaterialInfoReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 王巨壁
 * @title: StorageController
 * @projectName smart-sail
 * @description: 仓库管理
 * @date 2019/8/172:13
 */
@RestController()
@Api(tags ="仓库管理")
@RequestMapping("storage")
public class StorageController extends ControllerResponse {

    private IStorageRecordService iStorageRecordService;
    private IStorageCurrentInfoService iStorageCurrentInfoService;
    private IOrderItemService iOrderItemService;

    public StorageController(IStorageRecordService iStorageRecordService, IStorageCurrentInfoService iStorageCurrentInfoService, IOrderItemService iOrderItemService) {
        this.iStorageRecordService = iStorageRecordService;
        this.iStorageCurrentInfoService = iStorageCurrentInfoService;
        this.iOrderItemService = iOrderItemService;
    }

    @HeaderWrapper(ListMaterialStorageEnum.class)
    @ApiOperation(value = "原料库存列表 分页|currentPage|size|sortType|sortField 必传", notes = "原料库存列表 分页|currentPage|size|sortType|sortField 必传")
    @PostMapping("listMaterialStorage/{currentPage}/{size}/{sortType}/{sortField}")
    public ApiResponse<IPage<ListMaterialStorageResp>> listMaterialStorage(@RequestBody Map<String ,Object> queryParam,
                                                                           @PathVariable("currentPage")Long currentPage,
                                                                           @PathVariable("size")Integer size,
                                                                           @PathVariable("sortType")Integer sortType,
                                                                           @PathVariable("sortField")String sortField) {
        PageParam pageParam = new PageParam(currentPage,size,sortType,sortField);
        return ok(iStorageRecordService.listMateriaStorage(pageParam));

    }

    @ApiOperation(value = "获取原料库存详情", notes = "获取原料库存详情")
    @GetMapping("materialInfo/{itemId}")
    public ApiResponse<ItemStorageInfo> getMaterialInfo(@PathVariable("itemId")Long itemId) {
        return ok(iStorageCurrentInfoService.getStorageInfoByItemId(itemId,2));
    }

    @HeaderWrapper(ListProductsStorageEnum.class)
    @ApiOperation(value = "成品库存列表 分页|currentPage|size|sortType|sortField 必传", notes = "成品库存列表 分页|currentPage|size|sortType|sortField 必传")
    @PostMapping("listProductsStorage/{currentPage}/{size}/{sortType}/{sortField}")
    public ApiResponse<IPage<ListProductsStorageResp>> listProductStorage(@RequestBody Map<String ,Object> queryParam,
                                                                          @PathVariable("currentPage")Long currentPage,
                                                                          @PathVariable("size")Integer size,
                                                                          @PathVariable("sortType")Integer sortType,
                                                                          @PathVariable("sortField")String sortField) {
        PageParam pageParam = new PageParam(currentPage,size,sortType,sortField);
        return ok(iStorageRecordService.listProductStorage(pageParam));
    }


    @ApiOperation(value = "获取产品库存详情", notes = "获取产品库存详情")
    @GetMapping("productStorageInfo/{itemId}")
    public ApiResponse<ItemStorageInfo> getProductInfo(@PathVariable("itemId")Long itemId) {
        return ok(iStorageCurrentInfoService.getStorageInfoByItemId(itemId,1));
    }


    @ApiOperation(value = "修改原料库存设置", notes = "修改原料库存设置")
    @PostMapping("updateMaterialInfo")
    public ApiResponse<Object> updateMaterialInfo(@RequestBody UpdateMaterialInfoReq updateMaterialInfoReq) {

        StorageCurrentInfo info = new StorageCurrentInfo();
        BeanUtils.copyProperties(updateMaterialInfoReq, info);

        QueryWrapper<StorageCurrentInfo> query = new QueryWrapper<>();
        query.eq("itemId", updateMaterialInfoReq.getItemId());
        List<StorageCurrentInfo> list = iStorageCurrentInfoService.list(query);
        if (list != null && list.size() > 0) {
            StorageCurrentInfo currentInfo = list.get(0);
            BeanUtils.copyProperties(updateMaterialInfoReq, currentInfo);
            currentInfo.setStatus(CommonUtils.getStorageStatus(currentInfo.getSize(), currentInfo.getUpperLimit(), currentInfo.getLowerLimit()));
            iStorageCurrentInfoService.updateById(currentInfo);
        } else {
            StorageCurrentInfo storageCurrentInfo = new StorageCurrentInfo();
            BeanUtils.copyProperties(updateMaterialInfoReq, storageCurrentInfo);
            storageCurrentInfo.setStatus(StorageStatusEnum.BZ);
            iStorageCurrentInfoService.save(storageCurrentInfo);
        }

        return ok();
    }

    @ApiOperation(value = "根据orderItem获取该生产成品待入库的情况", notes = "根据orderItem获取该生产成品待入库的情况")
    @PostMapping("waitInStorageByOrderItemId/{orderItemId}")
    public ApiResponse<Object> waitInStorageByOrderItemId(@PathVariable("orderItemId") Long  orderItemId) {
        OrderItem byId = iOrderItemService.getById(orderItemId);
        QueryWrapper<StorageRecord> query = new QueryWrapper<>();
        query.eq("orderId", byId.getOrderId()).eq("itemId", byId.getItemId()).eq("status",StorageStatusEnum.SQRK.getCode());
        StorageRecord one = iStorageRecordService.getOne(query);
        if (one == null||one.getSize()==0) {
            return fail("该产品订单没有待入库成品!");
        }
        ListOrderItemRsp result = new ListOrderItemRsp();
        HashMap<String, Object> map = new HashMap<>();
        map.put("itemName", byId.getItemName());
        map.put("orderItemId", byId.getId());
        map.put("specification", byId.getSpecification());
        map.put("waitInSize", one.getSize());
        return ok(map);
    }

    @OperRecord(content = "生产成品申请入库",level = 1)
    @ApiOperation(value = "生产中的申请入库", notes = "生产中的申请入库")
    @PostMapping("sqrk/{orderItemId}/{size}")
    public ApiResponse<Object> sqrk(@PathVariable("orderItemId") Long  orderItemId,@PathVariable("size") Integer size) {
        OrderItem byId = iOrderItemService.getById(orderItemId);
        QueryWrapper<StorageRecord> query = new QueryWrapper<>();
        query.eq("orderId", byId.getOrderId()).eq("itemId", byId.getItemId()).eq("status",StorageStatusEnum.SQRK.getCode());
        StorageRecord one = iStorageRecordService.getOne(query);
        if (one == null||one.getSize()==0) {
            return fail("该产品订单没有待入库成品!");
        }
        if (one.getSize() > size) {
            one.setSize(one.getSize() - size);//剩下的保持不变
            iStorageRecordService.updateById(one);//剩下的还是申请入库
            one.setId(null);
            one.setSize(size);
            one.setStatus(StorageStatusEnum.DRK);//待入库
            one.setCreateTime(null);
            return ok(iStorageRecordService.save(one));
        } else {
            //如果全部申请入库  那这个orderItem 就没有可以入库的申请必要了  直接改成完成
            byId.setStatus(OrderItemStatusEnum.YWC);
            iOrderItemService.updateById(byId);
        }
        one.setStatus(StorageStatusEnum.DRK);//全部申请入库 . 此时没有申请入库状态的数据了.
        return ok(iStorageRecordService.updateById(one));
    }

}
